package com.rr.openApi.article.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.rr.frame.common.FinalDes;
import com.rr.frame.exception.ServiceException;
import com.rr.frame.utils.InitRequestParameter;
import com.rr.openApi.article.service.ArticleService;

@RestController
@RequestMapping("/openApi/article")
public class ArticleController {
	private Logger logger = Logger.getLogger(ArticleController.class);
	
	@Autowired
	private ArticleService articleService;
	
	@RequestMapping(value="/addArticle",method=RequestMethod.POST)
	 public Map<String,Object> add(HttpServletRequest request) {
		 Map<String, Object> resultMap = new HashMap<String, Object>();
		 Map<String, Object> paraMap = new HashMap<String, Object>();
		// Map<String, Object> parameter = new HashMap<String, Object>();
	     try {
	         	request.setCharacterEncoding("UTF-8");
	            paraMap = InitRequestParameter.initParameters(request);  
	            logger.info("添加文章参数输入=====>" + paraMap);
	            if(null==paraMap.get("title")||"".equals(paraMap.get("title"))){
	 				throw new ServiceException("FA_FAILED_TITLE_NOT_NULL", "文章标题不能为空");
	 			}
	            if(null==paraMap.get("tag")||"".equals(paraMap.get("tag"))){
		 			throw new ServiceException("FA_FAILED_TAG_NOT_NULL", "文章标签不能为空");
		 		}
	            if(null==paraMap.get("content")||"".equals(paraMap.get("content"))){
		 			throw new ServiceException("FA_FAILED_CONTENT_NOT_NULL", "文章内容不能为空");
		 		}
	            if(null==paraMap.get("uid")||"".equals(paraMap.get("uid"))){
		 			throw new ServiceException("FA_FAILED_UID_NOT_NULL", "文章作者不能为空");
		 		}
	            if(null==paraMap.get("summary")||"".equals(paraMap.get("summary"))){
		 			throw new ServiceException("FA_FAILED_SUMMARY_NOT_NULL", "文章summary不能为空");
		 		}
	           String data = articleService.addArticle(paraMap);
	           resultMap.put("data", data);
	           resultMap.put(FinalDes.STATUS_CODE, FinalDes.OK_CODE);
	           resultMap.put(FinalDes.STATUS_MSG, "");
	             
	     } catch (ServiceException e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, e.getStatus());
			resultMap.put(FinalDes.STATUS_MSG, e.getMessage());
			logger.info("ssss");
		 } catch (Exception e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, FinalDes.SYSTEM_ERROR_STATUS);
			resultMap.put(FinalDes.STATUS_MSG, FinalDes.SYSTEM_ERROR_INFO);
	     }
	     logger.info("添加文章参数输出=====>" + resultMap);
	     return resultMap;
	}
	
	@RequestMapping(value="/addLike",method=RequestMethod.POST)
	 public Map<String,Object> addLike(HttpServletRequest request) {
		 Map<String, Object> resultMap = new HashMap<String, Object>();
		 Map<String, Object> paraMap = new HashMap<String, Object>();
		// Map<String, Object> parameter = new HashMap<String, Object>();
	     try {
	         	request.setCharacterEncoding("UTF-8");
	            paraMap = InitRequestParameter.initParameters(request);  
	            logger.info("文章addLike参数输入=====>" + paraMap);
	            if(null==paraMap.get("aid")||"".equals(paraMap.get("aid"))){
		 			throw new ServiceException("FA_FAILED_AID_NOT_NULL", "文章id不能为空");
		 		}
	           String data = articleService.addLike(paraMap);
	           resultMap.put("data", data);
	           resultMap.put(FinalDes.STATUS_CODE, FinalDes.OK_CODE);
	           resultMap.put(FinalDes.STATUS_MSG, "");
	             
	     } catch (ServiceException e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, e.getStatus());
			resultMap.put(FinalDes.STATUS_MSG, e.getMessage());
			logger.info("ssss");
		 } catch (Exception e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, FinalDes.SYSTEM_ERROR_STATUS);
			resultMap.put(FinalDes.STATUS_MSG, FinalDes.SYSTEM_ERROR_INFO);
	     }
	     logger.info("文章addLike参数输出=====>" + resultMap);
	     return resultMap;
	}
	
	@RequestMapping(value="/getArticle",method=RequestMethod.POST)
	 public Map<String,Object> getArticle(HttpServletRequest request) {
		 Map<String, Object> resultMap = new HashMap<String, Object>();
		 Map<String, Object> paraMap = new HashMap<String, Object>();
		// Map<String, Object> parameter = new HashMap<String, Object>();
	     try {
	         	request.setCharacterEncoding("UTF-8");
	            paraMap = InitRequestParameter.initParameters(request);  
	            logger.info("获取文章参数输入=====>" + paraMap);
	            if(null==paraMap.get("aid")||"".equals(paraMap.get("aid"))){
		 			throw new ServiceException("FA_FAILED_AID_NOT_NULL", "文章id不能为空");
		 		}
	            Map<String, Object> data = articleService.getArticleByid(paraMap.get("aid").toString());
	           resultMap.put("data", data);
	           resultMap.put(FinalDes.STATUS_CODE, FinalDes.OK_CODE);
	           resultMap.put(FinalDes.STATUS_MSG, "");
	             
	     } catch (ServiceException e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, e.getStatus());
			resultMap.put(FinalDes.STATUS_MSG, e.getMessage());
			logger.info("ssss");
		 } catch (Exception e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, FinalDes.SYSTEM_ERROR_STATUS);
			resultMap.put(FinalDes.STATUS_MSG, FinalDes.SYSTEM_ERROR_INFO);
	     }
	     logger.info("获取文章参数输出=====>" + resultMap);
	     return resultMap;
	}
	
	@RequestMapping(value="/getArticletList",method=RequestMethod.POST)
	 public Map<String,Object> getArticleList(HttpServletRequest request) {
		 Map<String, Object> resultMap = new HashMap<String, Object>();
		 Map<String, Object> paraMap = new HashMap<String, Object>();
		// Map<String, Object> parameter = new HashMap<String, Object>();
	     try {
	         	request.setCharacterEncoding("UTF-8");
	            paraMap = InitRequestParameter.initParameters(request);  
	            logger.info("获取文章List参数输入=====>" + paraMap);
	            if(null==paraMap.get("start")||"".equals(paraMap.get("start"))){
		 			paraMap.put("start", 0);
		 		}
	            if(null==paraMap.get("num")||"".equals(paraMap.get("num"))){
		 			paraMap.put("num", 20);
		 		}
	           List<Map<String, Object>> data = articleService.getArticleList(paraMap);
	           resultMap.put("data", data);
	           resultMap.put(FinalDes.STATUS_CODE, FinalDes.OK_CODE);
	           resultMap.put(FinalDes.STATUS_MSG, "");
	             
	     } catch (ServiceException e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, e.getStatus());
			resultMap.put(FinalDes.STATUS_MSG, e.getMessage());
			logger.info("ssss");
		 } catch (Exception e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, FinalDes.SYSTEM_ERROR_STATUS);
			resultMap.put(FinalDes.STATUS_MSG, FinalDes.SYSTEM_ERROR_INFO);
	     }
	     logger.info("获取文章List参数输出=====>" + resultMap);
	     return resultMap;
	}
	
	@RequestMapping(value="/getArticletListByKeyWord",method=RequestMethod.POST)
	 public Map<String,Object> getArticletListByKeyWord(HttpServletRequest request) {
		 Map<String, Object> resultMap = new HashMap<String, Object>();
		 Map<String, Object> paraMap = new HashMap<String, Object>();
		// Map<String, Object> parameter = new HashMap<String, Object>();
	     try {
	         	request.setCharacterEncoding("UTF-8");
	            paraMap = InitRequestParameter.initParameters(request);  
	            logger.info("获取文章List参数输入=====>" + paraMap);
	            if(null==paraMap.get("key")||"".equals(paraMap.get("key"))){
		 			throw new ServiceException("FA_FAILED_KEY_NOT_NULL", "查询关键字不能为空");
		 		}
	            if(null==paraMap.get("start")||"".equals(paraMap.get("start"))){
		 			paraMap.put("start", 0);
		 		}
	            if(null==paraMap.get("num")||"".equals(paraMap.get("num"))){
		 			paraMap.put("num", 20);
		 		}
	           List<Map<String, Object>> data = articleService.getArticleListByKeyWord(paraMap);
	           resultMap.put("data", data);
	           resultMap.put(FinalDes.STATUS_CODE, FinalDes.OK_CODE);
	           resultMap.put(FinalDes.STATUS_MSG, "");
	             
	     } catch (ServiceException e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, e.getStatus());
			resultMap.put(FinalDes.STATUS_MSG, e.getMessage());
			logger.info("ssss");
		 } catch (Exception e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, FinalDes.SYSTEM_ERROR_STATUS);
			resultMap.put(FinalDes.STATUS_MSG, FinalDes.SYSTEM_ERROR_INFO);
	     }
	     logger.info("获取文章List参数输出=====>" + resultMap);
	     return resultMap;
	}
}
